package com.example.lenovo.appstore.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.example.lenovo.appstore.R;
import com.example.lenovo.appstore.utils.AppContext;
import com.example.lenovo.appstore.utils.LanguagePreUtils;
import com.example.lenovo.appstore.utils.LanguageUtil;
import com.example.lenovo.appstore.utils.ScreenSizeUtils;

import java.util.Locale;

import androidx.annotation.Nullable;
import butterknife.Bind;
import butterknife.ButterKnife;


public class TitleBarView extends LinearLayout implements View.OnClickListener {
    @Bind(R.id.title_back)
    ImageView titleBack;
    @Bind(R.id.tv_titlebar_center)
    TextView titleView;
    @Bind(R.id.tv_titlebar_right)
    TextView tvTitlebarRight;
//    @Bind(R.id.add_addressView)
//    ImageView addAddressView;

    private Context context;
    private boolean isEnglish=false;
    private Activity activity;

    public TextView getTvTitlebarRight() {
        return tvTitlebarRight;
    }

    public void setTvTitlebarRight(TextView tvTitlebarRight) {
        this.tvTitlebarRight = tvTitlebarRight;
    }
    public TitleBarView(Activity activity) {
        super(activity);
        this.activity = activity;
    }

    public TitleBarView(Context context) {
        super(context);
        this.context = context;
        initView();
    }

    public TitleBarView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initView();


    }

    public TitleBarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView();
    }


    private void initView() {
        View view = LayoutInflater.from(context).inflate(R.layout.index_title_button, this);
        ButterKnife.bind(view);
        //返回按钮
        titleBack.setOnClickListener(this);
        tvTitlebarRight.setOnClickListener(this);
        // 添加地址
//        addAddressView.setOnClickListener(this);


    }

/**SharedPreUtils.setStringToPre(LoginActivity.this,
            "phone", bean.getMsg().getUser_phone());
                        SharedPreUtils.setStringToPre(LoginActivity.this,
            "token", bean.getMsg().getToken());
                        SharedPreUtils.setStringToPre(LoginActivity.this,
            "nickname", bean.getMsg().getNickname());*/
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_back:
                ((Activity) context).onBackPressed();
                break;
            case R.id.tv_titlebar_right:
                showDialog();
//                SharedPreUtils.clearDatas(context);
////                SharedPreUtils.setStringToPre(context,"username","");
//                String phone= SharedPreUtils.getPreStringInfo(AppContext.getInstance(), "phone");
//                String name= SharedPreUtils.getPreStringInfo(AppContext.getInstance(), "nickname");
//                String token= SharedPreUtils.getPreStringInfo(AppContext.getInstance(), "token");
//                Log.e("====",phone+"====\n"+name+"===\n"+token);
                break;


        }
    }

    private void set(String lauType) {
        // 本地语言设置
        Locale myLocale = new Locale(lauType);
        Resources res = AppContext.getInstance().getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        Configuration conf = res.getConfiguration();
        conf.locale = Locale.US;
        res.updateConfiguration(conf, dm);
    }

    Dialog dialog;
    private void showDialog(){
        dialog = new Dialog(getContext(), R.style.NormalDialogStyle);
        View view = View.inflate(getContext(), R.layout.dialog_bottom, null);
        dialog.setContentView(view);
        Button changeLanguage=(Button) view.findViewById(R.id.changeLanguage);
//        Button changePWD=(Button) view.findViewById(R.id.changePWD);
        Button cancel=(Button) view.findViewById(R.id.cancel);
        changeLanguage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
//                LanguagePreUtils.(SettingAc.this, locals[which]);
                String language=LanguagePreUtils.getPreStringInfo(AppContext.getInstance(),"language");
                Log.e("language==",language);
                LanguageUtil util=new LanguageUtil(AppContext.getInstance());
                if(TextUtils.isEmpty(language)||language.equals("zh")){
                    LanguagePreUtils.setStringToPre(AppContext.getInstance(),"language","en");
//                    EventBus.getDefault().post("EVENT_REFRESH_LANGUAGE");
                    util.changeAppLanguage(language);

                }else{
                    LanguagePreUtils.setStringToPre(AppContext.getInstance(),"language","zh");
//                    EventBus.getDefault().post("EVENT_REFRESH_LANGUAGE");
                    util.changeAppLanguage(language);

                }
                dialog.dismiss();

//                if(isEnglish){
//                    set("CHINESE");
//                    isEnglish=false;
//                }else{
//                    set("ENGLISH");
//                    isEnglish=true;
//                }
//                dialog.dismiss();
            }
        });
//        changePWD.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Intent intent=new Intent(AppContext.getInstance(),FindPassWordActivity.class);
//                getContext().startActivity(intent);
//                dialog.dismiss();
////                activity.finish();
//
//            }
//        });
        cancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        dialog.setCanceledOnTouchOutside(true);
        view.setMinimumHeight((int) (ScreenSizeUtils.getInstance(AppContext.getInstance()).getScreenHeight() * 0.23f));
        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (ScreenSizeUtils.getInstance(AppContext.getInstance()).getScreenWidth() * 0.9f);
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.BOTTOM;
        dialogWindow.setAttributes(lp);
//        if ((new Activity(context)).isFinishing()) {
            dialog.show();
//        } else {
//            return;
//        }
    }
    private void finish(Activity activity){
        activity.finish();
    }
    /**
     * 设置页面标题
     *
     * @param title
     */
    public void setTitle(String title) {
        titleView.setText(title);
    }

    public void setEditTitle(String title) {
        tvTitlebarRight.setVisibility(View.VISIBLE);
        tvTitlebarRight.setText(title);
    }

//    /**
//     * 显示添加地址按钮
//     */
//    public void showAddAddressButton(boolean isShow) {
//        if (isShow) {
//            addAddressView.setVisibility(View.VISIBLE);
//        } else {
//            addAddressView.setVisibility(View.GONE);
//
//        }
//    }

    /**
     * 显示编辑按钮
     *
     * @param isShow
     */
    public void showEditButton(boolean isShow) {
        if (isShow) {
            tvTitlebarRight.setVisibility(View.VISIBLE);
        } else {
            tvTitlebarRight.setVisibility(View.GONE);
        }
    }

    /**
     * 显示编辑按钮
     *
     * @param isShow
     */
    public void showBackButton(boolean isShow) {
        if (isShow) {
            titleBack.setVisibility(View.VISIBLE);
        } else {
            titleBack.setVisibility(View.GONE);
        }
    }
}
